/*
 * @(#)org.example.netty.http 2020/4/1/0001
 *
 * Copy Right@ 人人译视界
 */


package org.example.netty.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * <pre>
 * @author tuwei
 *
 * @version 0.9
 *
 * 修改版本: 0.9
 * 修改日期: 2020/4/1/0001
 * 修改人 :  tuwei
 * 修改说明: 初步完成
 * 复审人 ：
 * </pre>
 */
public class TestServer {
	public static void main(String[] args) throws Exception {
		NioEventLoopGroup bossGroup = new NioEventLoopGroup();
		NioEventLoopGroup workerGroup = new NioEventLoopGroup();

		try {
			ServerBootstrap bootstrap = new ServerBootstrap();
			bootstrap.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class)// 使用NioSocketChannel作为服务器的通道实现
					.option(ChannelOption.SO_BACKLOG, 128)// 设置线程队列得到连接个数
					.childOption(ChannelOption.SO_KEEPALIVE, true)// 设置保持活动连接状态
					.childHandler(new TestServerInitializer());
			ChannelFuture channelFuture = bootstrap.bind(8000).sync();
			channelFuture.addListener(new ChannelFutureListener() {
				@Override
				public void operationComplete(ChannelFuture channelFuture) throws Exception {
					if (channelFuture.isSuccess()) {
						System.out.println("监听端口 8000 成功");
					} else {
						System.out.println("监听端口 8000 失败");
					}
				}
			});
			// 对关闭通道的事件进行监听
			channelFuture.channel().closeFuture().sync();
		}
		finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}
